package com.appshelves.model;

import java.util.List;

import com.appshelves.model.saveable.SaveableList;

public class Model extends ModelAPI {

    public static enum Event {
        SHELF_LOADED, AFTER_LOAD, SHELF_ADDED_TO_TOP, SHELF_ADDED_TO_BOTTOM, SHELF_DELETED, THERE_ARE_SHELVES,
        THERE_ARENT_SHELVES, OFF_SHELF_APP_ADDED, OFF_SHELF_APP_DELETED, APP_UPDATED, APP_UNINSTALLED,
        SHELF_MOVED_TO_TOP,
        ///
        APPS_ADDED_TO_SHELF, APP_MOVED_TO_SHELF, APPS_REMOVED_FROM_SHELF, APP_REMOVED_FROM_SHELF, APP_MOVED_TO_TOP,
    };

    public static interface EventListener {
        public void onEvent(Object args);
    }

    public static void addListener(Event event, EventListener listener) {
        getEvents().addListener(event, listener);
    }

    public static void addListener(Shelf shelf, Event event, EventListener listener) {
        getEvents().addListener(shelf, event, listener);
    }

    public static boolean isListenerSet(Event event) {
        return getEvents().isListenerSet(event);
    }

    public static void removeListener(Event event, EventListener listener) {
        getEvents().removeListener(event, listener);
    }

    public static void removeAllListeners() {
        getEvents().removeAllListeners();
    }

    ///

    public static void assureLoaded() {
        getModel().assureLoaded();
    }

    public static void save() {
        getModel().save();
    }

    public static void rebuildAppData() {
        getCreator().rebuildAppData();
    }

    ///

    public static int getShelvesNumber() {
        return getAccessor().getRealShelvesNumber();
    }

    public static Shelf getShelf(int index) {
        return getAccessor().getShelf(index);
    }

    public static void deleteShelf(Shelf shelf) {
        getModel().deleteShelf(shelf);
    }

    public static List<App> getShelfApps(Shelf shelf) {
        return getAccessor().getShelfApps(shelf);
    }

    public static Object getShelfAppsNumber(Shelf shelf) {
        return getAccessor().getShelfAppsNumber(shelf);
    }

    public static int getShelfAppPosition(Shelf shelf, App app) {
        return getAccessor().getShelfAppPosition(shelf, app);
    }

    ///

    public static List<App> getOffShelfApps() {
        return getAccessor().getOffShelfApps();
    }

    public static int getOffShelfAppsNumber() {
        return getAccessor().getOffShelfAppsNumber();
    }

    public static boolean isOffShelfApp(App app) {
        return getAccessor().isOffShelfApp(app);
    }

    public static int getOffShelfAppPosition(App app) {
        return getAccessor().getOffShelfAppPosition(app);
    }

    ///

    public static void requestChange(Shelf shelf) {
        getModel().requestChange(shelf);
    }

    public static Shelf getShelfToChange() {
        return getModel().getShelfToChange();
    }

    public static void requestAddAppsToShelf(List<App> apps) {
        getModel().requestAddAppsToShelf(apps);
    }

    public static void requestRemoveAppsFromShelf(List<App> apps) {
        getModel().requestRemoveAppsFromShelf(apps);
    }

    public static void requestDeleteShelf() {
        getModel().requestDeleteShelf();
    }

    public static boolean performChangeIfNeeded() {
        return getModel().performChangeIfNeeded();
    }

    ///

    public static void onAppClicked(App app) {
        getModel().onAppClicked(app);
    }

    public static void moveAppToShelf(App app, Shelf shelf) {
        getModel().moveAppToShelf(app, shelf);
    }

    public static void moveAppToNewShelf(App app, boolean toBottom) {
        getModel().moveAppToNewShelf(app, toBottom);
    }

    public static void deleteApp(String packageName) {
        getModel().deleteApp(packageName);
    }

    public static void updateOrAddApp(String packageName) {
        getModel().updateOrAddApp(packageName);
    }

    /// debug 

    public static void eraseAppData() {
        SaveableList.eraseDataFile(AppData.class);
    }

    public static void log() {
        getModel().log();
    }

}
